package com.bimbimbip.explorer;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

class ProgressThread extends Thread {
	Handler mHandler;
	final static int STATE_DONE = 0;
	final static int STATE_RUNNING = 1;
	String src, tar;
	int mState, total = 0, counting;
	Activity parent;

	private void startCopy(String src, String tar) {

		if (mState != STATE_RUNNING)
			return;
		File fs = new File(src);
		File fd = new File(tar);
		if (fs.isFile()) {
			Message msg = mHandler.obtainMessage();
			Bundle b = new Bundle();
			b.putString("current", fs.getName());
			b.putInt("total", total);
			b.putInt("count", ++counting);
			msg.setData(b);
			mHandler.sendMessage(msg);
			if (!copyfile(fs, fd)) {
				Toast.makeText(parent, "Copying failed !", Toast.LENGTH_LONG)
						.show();
				mState = STATE_DONE;
			} else {

			}
		} else {
			if (!fd.exists() & !fd.mkdir()) {
				Toast.makeText(parent, "Create directory failed !",
						Toast.LENGTH_LONG).show();
				mState = STATE_DONE;
			}
			for (String str : fs.list()) {
				if (mState != STATE_RUNNING)
					return;
				startCopy(src + "/" + str, tar + "/" + str);
			}
		}
	}

	private boolean copyfile(File f1, File f2) {

		try {
			InputStream in = new FileInputStream(f1);
			OutputStream out = new FileOutputStream(f2);
			byte[] buf = new byte[1024];
			int len;
			while ((len = in.read(buf)) > 0) {
				out.write(buf, 0, len);
			}
			in.close();
			out.close();
			System.out.println("Copy OK");
			return true;
		} catch (Exception ex) {

			return false;
		}
	}

	private void preparing(File file) {
		if (file.list() != null)
			for (String str : file.list()) {
				File f = new File(file.getAbsolutePath() + "/" + str);
				if (f.isFile())
					total++;
				else if (f.isDirectory())
					preparing(f);
			}
	}

	public void run() {
		mState = STATE_RUNNING;
		preparing(new File(src));
		if (total == 0)
			total = 1;
		startCopy(src, tar);
		parent.finish();
	}

	public void setState(int state) {
		mState = state;
	}

	public ProgressThread(Handler h, String src, String tar, Activity act) {
		mHandler = h;
		this.src = src;
		this.tar = tar;
		parent = act;
	}
}

public class FolderCopy extends Activity {
	boolean stop = false;
	String src, tar;
	TextView nameView, sourceView, targetView, currentView, progressView;
	ProgressThread progressThread;
	ProgressBar progressBar;
	Button stopCopy;

	protected void onStart() {
		super.onStart();
		progressBar.setMax(100);
		progressBar.setScrollBarStyle(ProgressDialog.STYLE_HORIZONTAL);
		System.out.println("Start copy");
		progressThread = new ProgressThread(handler, src, tar, this);
		progressThread.start();

	}

	final Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			currentView.setText("Current : "
					+ msg.getData().getString("current"));
			int total = msg.getData().getInt("total");
			progressBar.setMax(total);
			int progress = msg.getData().getInt("count");
			progressBar.setProgress(progress);
			System.out.println(progress + "   " + total);
			progressView.setText(" " + (100 * progress / total) + "% done.    "
					+ progress + "/" + total + " file(s)");
		}
	};

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.folder_copy);
		Intent intent = getIntent();
		src = intent.getExtras().getString("foldersource");
		tar = intent.getExtras().getString("foldertarget");

		File file = new File(src);
		File ftar = new File(tar);

		nameView = (TextView) findViewById(R.id.copyfilename);
		sourceView = (TextView) findViewById(R.id.copyfilefrom);
		targetView = (TextView) findViewById(R.id.copyfileto);
		progressView = (TextView) findViewById(R.id.copyfilepro);
		currentView = (TextView) findViewById(R.id.copyfilecur);

		nameView.setText("Copy file : " + file.getName());
		sourceView.setText("From : " + file.getParent());
		targetView.setText("To : " + ftar.getParent());
		currentView.setText("Current : ");
		progressBar = (ProgressBar) findViewById(R.id.copyProgress);
		stopCopy = (Button) findViewById(R.id.stopCopy);
		stopCopy.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				progressThread.mState = ProgressThread.STATE_DONE;
				stop = true;
				finish();
			}
		});
	}

}
